package com.cn.qgx428.lazydiary.ui.activity

import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.cn.qgx428.lazydiary.APP
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.dialog.CustomAlertDialog
import com.cn.qgx428.lazydiary.dialog.InputDialog
import com.cn.qgx428.lazydiary.dialog.LoadingDialog
import com.jaeger.library.StatusBarUtil
import java.util.*

/**
 * Created by QinX on 2017/12/12.
 */
abstract class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        APP.addActivity(this)
        setStatuBarColor(Color.BLACK)
        initUI()
    }

    override fun onDestroy() {
        super.onDestroy()
        APP.removeAcitivity(this)
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.keep, R.anim.from_right_out)
    }

    override fun startActivity(intent: Intent?) {
        super.startActivity(intent)
        overridePendingTransition(R.anim.from_right_in, R.anim.keep)
    }

    /**
     * 默认手机返回键退出当前Activity 。
     */
    override fun onBackPressed() {
        this.finish()
    }

    //让子类决定布局
    abstract fun initUI();


    protected fun setStatuBarTranslucent(alpha: Int) = StatusBarUtil.setTranslucent(this, alpha)
    protected fun setStatuBarColor(color: Int, alpha: Int=255) = StatusBarUtil.setColor(this,color,alpha)
    protected fun setStatuBarTransparent() = StatusBarUtil.setTransparent(this)
    protected fun setStatuBarDiff(alpha: Int) = StatusBarUtil.setColorDiff(this,alpha)

    protected fun hindActionBar() = supportActionBar?.hide()

    /**
     * 取消状态栏
     */
    protected fun cancelStateBar(){
        window.requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        }
    }

    //protected fun toast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
    protected fun log(message: String) = Log.d(this.localClassName, message)

    //动画相关
    //设置入场动画
    protected fun launchAnim(first: Int, second: Int = R.anim._activity_conteract) = overridePendingTransition(first, second)
    //设置关闭动画
    protected fun endAnim(second: Int, first: Int=0) = overridePendingTransition(first, second)

    //检查网络状态
    public fun checkNetworkStatus(): Boolean{
        val connectManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE)
            as ConnectivityManager
        val networkInfor = connectManager.allNetworkInfo
        networkInfor.forEach {
            if (it.state == NetworkInfo.State.CONNECTED)
                return true
        }

        return false
    }

    //提示框
    public fun getCustomAlertDialog(): CustomAlertDialog{
        return CustomAlertDialog(this)
    }
    //时间选取框
    public fun getDatePickerDialog(dateListener: DatePickerDialog.OnDateSetListener): DatePickerDialog{
        val calendar = Calendar.getInstance(Locale.CHINA)
        val year = calendar.get(Calendar.YEAR)
        val mouth = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        return DatePickerDialog(this, dateListener,year,mouth,day)
    }
    // 时间选取框 —— 小时-分
    public fun getTimePickerDialog(timeListener: TimePickerDialog.OnTimeSetListener): TimePickerDialog{
        val calendar = Calendar.getInstance(Locale.CHINA)
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        return TimePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, timeListener,hour, minute,true)
    }

    //输入框
    public fun getInputDialog(): InputDialog{
        return InputDialog(this)
    }
    //输入框
    public fun getInputDialog(commit: (msg: String)->Unit): InputDialog{
        val dialog = getInputDialog()
        dialog.setCommit(commit)
        return dialog
    }

    //加载框
    public fun getLoadingDialog(): LoadingDialog{
        return LoadingDialog(this)
    }
}